#include "MultiObject.h"

MultiObject::MultiObject()
{
	numObject = 0;
	maxObject = 0;
	trackingObject = NULL;
	ID = NULL;
}

MultiObject::MultiObject( int maxObject )
{
	this->maxObject = maxObject;
	ID = new int[maxObject];
	trackingObject = new Object[maxObject]; 
}

MultiObject::~MultiObject()
{
	if(ID != NULL)
	{
		delete[] ID;
		ID = NULL;
	}

	if(trackingObject != NULL)
	{
		delete[] trackingObject;
		trackingObject = NULL; 
	}
}

void MultiObject::init(Mat &frame, Object* allObject, int numObject)
{
	this->frame = frame.clone();

	for(int i = 0; i < maxObject; i++)
	{
		if(i < numObject)
		{
			ID[i] = i;
			trackingObject[i] = allObject[i];
		}else 
			ID[i] = -1;
	}
}

int MultiObject::getNumObject()
{
	int number = numObject;
	return number;
}

int MultiObject::getMaxObject()
{
	int number = maxObject;
	return maxObject;
}

void MultiObject::getID( int *ID )
{
	for(int i = 0; i < maxObject; i++)
		ID[i] = this->ID[i];
}

void MultiObject::predict(Object* allObject, int numObject)
{
	for(int i = 0; i < maxObject; i++)
	{
		if(ID[i] >= 0)
		{
			trackingObject[i].predict(allObject, numObject);
		}
	}
}


void MultiObject::measure( const Mat &frame, Object* allObject, int numObject )
{
	for(int i = 0; i < maxObject; i++)
	{
		if(ID[i] >= 0)
		{
			trackingObject[i].measure(this->frame, frame, allObject, numObject);
		}
	}
}

void MultiObject::correct( const Mat &frame, int numObject )
{
	for(int i = 0; i < maxObject; i++)
	{
		if(ID[i] >= 0)
		{
			trackingObject[i].correct(this->frame, frame, numObject);
		}
	}
}

void MultiObject::update( const Mat &frame )
{
	this->frame = frame.clone();
}

void MultiObject::render()
{
	for(int i = 0; i < maxObject; i++)
	{
		char buf[5];
		if(ID[i] >= 0)
		{
			itoa(i, buf, 10);
			rectangle(frame, trackingObject[i].getTopLeft(), trackingObject[i].getDownRight(), cvScalar(0, 0, (200*i)%255));
			putText(frame, buf,trackingObject[i].getCentroid(),5, 2, cvScalar(255, 255, 255));

			circle(frame, trackingObject[i].getCentroidPredict(), 5, Scalar(255, 255,255), CV_FILLED);	//white, predict position
			circle(frame, trackingObject[i].getCentroidMeasure(), 5, Scalar(0, 0,255), CV_FILLED);		// red,   measure position 
			circle(frame, trackingObject[i].getCentroid(), 5, Scalar(255, 0,0), CV_FILLED);				//blue,  correct position
		}
	}

	imshow("tracking ", frame);
}

